// interceptMarketBuy
TrvnInterceptorService.installInterceptor( {
	name : "market.MarketBuy",
	isAvailble : function() {
		return true;
	},
	intercept : function(doc) {
		if (!doc.URL.contains("build.php"))
			return;
		var params = doc.URL.toQueryParams();
		if (!doc.pageTitle.contains("市场") || !params.t || params.t != 1 || (params.a && params.g))
			return;
		var tables = doc.evaluateXPath(TrvnCfg.queryXPath("marketBuyTable"));
		var table = tables[0].rows.length == 2 ? tables[1] : tables[0];
		if (!table)
			return;
		table.deleteRow(0);
		table.rows[0].cells[0].colSpan = 1;
		table.rows[0].cells[1].colSpan = 1;
		var cell = table.rows[0].insertCell(0);
		cell.innerHTML = "类型";
		cell = table.rows[0].insertCell(2);
		cell.innerHTML = "类型";
		var thead = table.createTHead();
		thead.className = "sort-header";
		thead.appendChild(table.rows[0]);
		var div1 = doc.$C("div");
		div1.style.textAlign = "right";
		div1.innerHTML = table.rows[table.rows.length - 1].cells[0].innerHTML;
		table.parentNode.insertBefore(div1, table);
		var div2 = doc.$C("div");
		div2.style.textAlign = "right";
		div2.innerHTML = table.rows[table.rows.length - 1].cells[0].innerHTML;
		table.parentNode.appendChild(div2);
		table.deleteRow(table.rows.length - 1);
		removeInvalidRows(table);

		/*
		 * var aUrl = doc.URL.substr(0,doc.URL.indexOf("?")+1); params.u =
		 * params.u?params.u:0; TrvnUtils.each(params,function(key,value){
		 * if(key!="u") aUrl += key+"="+value+"&"; });
		 * doc.parseUrl(aUrl+"u="+(params.u+40), new
		 * GetMarketDataParseCallback(function(t){ for(var i=0; i<t.rows.length;
		 * i++){ table.insertRow(-1).innerHTML = t.rows[i].innerHTML; } }));
		 * doc.parseUrl(aUrl+"u="+(params.u+80), new
		 * GetMarketDataParseCallback(function(t){ for(var i=0; i<t.rows.length;
		 * i++){ table.insertRow(-1).innerHTML = t.rows[i].innerHTML; } }));
		 * doc.parseUrl(aUrl+"u="+(params.u+120), new
		 * GetMarketDataParseCallback(function(t){ for(var i=0; i<t.rows.length;
		 * i++){ table.insertRow(-1).innerHTML = t.rows[i].innerHTML; } }));
		 */

		var sTable = new SortedTable(table, {
			getColumnComparator : function(colIndex) {
				return {
					compare : function(o1, o2) {
						if (colIndex == 1 || colIndex == 3) {
							o1 = TrvnUtils.parseNumber(o1);
							o2 = TrvnUtils.parseNumber(o2);
						}
						if (o1 < o2)
							return -1;
						if (o2 < o1)
							return 1;
						return 0;
					}
					,
				};
			},
			getSortableValueAt : function(rowObj, cellObj) {
				return cellObj.innerHTML;
			},
			isColumnSortable : function(colIndex) {
				return colIndex != 6;
			}
		});
	}
});
function removeInvalidRows(table) {
	var child = table.tBodies[0].rows[0];
	while (child && child.tagName == "TR") {
		var c = child.nextSibling;
		if (child.cells[6] && child.cells[6].className == "c") {
			table.deleteRow(child.rowIndex);
		}
		child = c;
	}
}
function GetMarketDataParseCallback(callback) {
	this.doParse = function(parseDoc) {
		try {
			var tables = parseDoc.evaluateXPath(TrvnCfg.queryXPath("marketBuyTable"));
			var table = tables[0].rows.length == 2 ? tables[1] : tables[0];
			if (!table)
				return;
			table.deleteRow(0);
			table.deleteRow(0);
			table.deleteRow(table.rows.length - 1);
			removeInvalidRows(table);
			callback(table);
		} catch (e) {
			TrvnUtils.Logger.error("sort-table.js 401: " + e);
		}
	}
}
// intercept market max avg
TrvnInterceptorService.installInterceptor( {
	name : "market.MarketMaxAvg",
	isAvailble : function() {
		return false;
	},
	intercept : function(doc) {
	}
});